
const address = {
  state: {
    area: []
  },
  mutations: {
    SET_PROVINCE: (state, province) => {
      state.area = province
    },

    SET_CITY: (state, { provinceCode, cities }) => {
      const province = state.area.find(value => value.value === provinceCode)
      province.children = cities
    },

    SET_COUNTY: (state, { provinceCode, cityCode, counties }) => {
      const province = state.area.find(value => value.value === provinceCode)
      const city = province.children.find(value => value.value === cityCode)
      if (city) {
        city.children = counties
      }
    }
  },
  actions: {
    // async setProvince({ commit }) {
    //   const { pros } = await queryProvince()
    //   const provinces = pros.map(value => ({
    //     value: value.code,
    //     label: value.name,
    //     id: value.province_id,
    //     children: []
    //   }))
    //   commit('SET_PROVINCE', provinces)
    //   return provinces
    // },
    //
    // async setCity({ commit }, provinceCode) {
    //   const { citys } = await queryCity(provinceCode)
    //   const cities = citys.map(value => ({
    //     value: value.code,
    //     label: value.name,
    //     id: value.city_id,
    //     children: []
    //   }))
    //   commit('SET_CITY', { provinceCode, cities })
    //   return cities
    // },
    //
    // async setCounty({ commit }, { provinceCode, cityCode }) {
    //   const { countys } = await queryCounty(cityCode)
    //   const counties = countys.map(value => ({
    //     value: value.code,
    //     label: value.name,
    //     id: value.county_id
    //   }))
    //   commit('SET_COUNTY', { provinceCode, cityCode, counties })
    //   return counties
    // }
  }
}

export default address
